package com.game.components
{
	import com.game.render.RenderData;
	import com.game.scene.SceneObject;
	import com.utils.Val;

	import flash.display.BlendMode;
	import flash.geom.ColorTransform;


	public class RenderComponent extends FrameComponent
	{
		protected var sceneObj : SceneObject;

		public var render : RenderData;

		public var zIndex : int = Val.LAYER_ROLE;

		/**
		 * 偏移x,y坐标
		 */
		public var offsetX : int;

		public var offsetY : int;

		private var _visible : Boolean;

		public function RenderComponent(type : *)
		{
			super(type == null ? RenderComponent : type);
		}

		override public function notifyAdded() : void
		{
			super.notifyAdded();
			render = new RenderData();
			render.zIndex = zIndex;
			sceneObj = owner as SceneObject;
			sceneObj.addRender(render);
		}

		public function set blendMode(blendMode : String) : void
		{
			if (render)
				render.blendMode = blendMode ? blendMode : BlendMode.NORMAL;
		}

		public function set filters(value : Array) : void
		{
			if (render)
				render.filters = value;
		}

		public function get filters() : Array
		{
			if (render)
				return render.filters;
			return null;
		}

		public function set colorTransform(value : ColorTransform) : void
		{
			if (render)
			{
				render.colorTransform = value;
			}
		}

		public function get colorTransform() : ColorTransform
		{
			if (render)
				return render.colorTransform;
			return null;
		}

		override public function update(delay : uint) : void
		{
			if (sceneObj)
			{
				updateRender(sceneObj.mapX, sceneObj.mapY, offsetX, offsetY);
			}
		}

		public function updateRender(sceneX : int, sceneY : int, offsetX : int = 0, offsetY : int = 0) : void
		{

		}

		/**
		 * 是否可见
		 * @return
		 *
		 */
		public function get visible() : Boolean
		{
			return _visible;
		}

		public function set visible(value : Boolean) : void
		{
			_visible = value;

			if (render)
				render.visible = _visible;
		}

		public function set alpha(value : Number) : void
		{
			if (render)
				render.alpha = value;
		}

		override public function destroy() : void
		{
			super.destroy();

			if (sceneObj)
			{
				sceneObj.removeRender(render);

				if (sceneObj.id == -1)
				{
					sceneObj.destroy();
				}
				sceneObj = null;
			}

			if (render)
			{
				render.destory();
				render = null;
			}
			offsetX = 0;
			offsetY = 0;
			_visible = false;
			zIndex = Val.LAYER_ROLE;
		}

	}
}